package site.zhouinfo.findpath;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @author zhou
 * @create 2017-10-24 11:31
 * @email zhouinfo@qq.com
 * @blog blog.zhouinfo.site
 */
public class NodePointList {

    ArrayList<NodePoint> open = new ArrayList<NodePoint>(10);
    ArrayList<NodePoint> close = new ArrayList<NodePoint>(10);
    ArrayList<NodePoint> path = new ArrayList<NodePoint>(10);

    private NodePoint end;

    private int startSize = 0;

    public NodePointList() {
    }

    public void add(NodePoint nodePoint) {
        this.startSize++;
        nodePoint.setH(clacH(nodePoint));
        nodePoint.clacF();
        open.add(nodePoint);
    }

    private int clacH(NodePoint n) {
        return Math.abs(n.getX() - end.getX()) + Math.abs(n.getY() - end.getY());
    }

    public NodePoint get() {
        this.startSize--;
        Collections.sort(open);
        NodePoint n = open.remove(0);
        close.add(n);
        return n;
    }

    public boolean isCloseList(NodePoint n) {
        return close.contains(n);
    }

    public int getSize() {
        return this.startSize;
    }

    public ArrayList<NodePoint> getOpen() {
        return open;
    }

    public void setOpen(ArrayList<NodePoint> open) {
        this.open = open;
    }

    public ArrayList<NodePoint> getClose() {
        return close;
    }

    public void setClose(ArrayList<NodePoint> close) {
        this.close = close;
    }

    public ArrayList<NodePoint> getPath() {
        return path;
    }

    public void setPath(ArrayList<NodePoint> path) {
        this.path = path;
    }

    public NodePoint getEnd() {
        return end;
    }

    public void setEnd(NodePoint end) {
        this.end = end;
    }

    public void print(NodePoint n) {
        if (n.getG() == 0) {
            System.out.println(n);
        } else {
            print(n.getParentNode());
            System.out.println(n);
        }
    }

    public void getPath(NodePoint n) {
        if (n.getG() == 0) {
            path.add(n);
        } else {
            getPath(n.getParentNode());
            path.add(n);
        }
    }
}
